/*
Copyright 2010,2011 Matt Van Der Westhuizen

This file is part of CGLL.

CGLL is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

CGLL is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License
along with CGLL.  If not, see <http://www.gnu.org/licenses/>.
*/

package org.chaoticengine.cgll.entity.component.tween;

import java.util.ArrayList;
import org.chaoticengine.cgll.entity.Entity;

/**
 *
 * @author Matt Van Der Westhuizen <mattpwest@gmail.com>
 */
public class FlickerTween extends AlphaTween {
    protected ArrayList<Integer> dimPoints = new ArrayList<Integer>();
    protected int dimPoint = 0;
    protected int flashes = 1;

    public FlickerTween(float startAlpha, float endAlpha, int duration, int flashes) {
        super(startAlpha, endAlpha, duration);

        this.flashes = flashes;

        findDimmingPoints();
    }

    @Override
    public void doUpdate(int delta) {
        if (isDone()) {
            return;
        }

        if (repeat.equals(RepeatMode.None)) {
            // Do nothing
        } else if (repeat.equals(RepeatMode.Loop)) {
            // Restart
            if (timePassed >= duration) {
                timePassed = 0;
            }
        } else if (repeat.equals(RepeatMode.PingPong)) {
            // Swop start & end values and restart
            if (timePassed >= duration) {
                timePassed = 0;

                float f = eAlpha;
                sAlpha = eAlpha;
                eAlpha = f;
            }
        }

        // Restart the animation
        if (dimPoint >= dimPoints.size()) {
            dimPoint = 0;
            changeAlpha(sAlpha);
        }

        // Start the animation
        if (dimPoint == 0) {
            dimPoint = 1;
            changeAlpha(sAlpha);
        }

        // Dim the alpha again at certain points
        Entity e = getOwner().getOwner();
        if (timePassed >= dimPoints.get(dimPoint)) {
            dimPoint++;

            float a = e.getAlpha();

            // Dim by between 30% and 100%
            a += (sAlpha - a) * ((flashes - dimPoint) / (float) flashes);

            e.setAlpha(a);
        }

        // Linear change from current to end alpha
        float a = e.getAlpha();
        changeAlpha(a +
                (eAlpha - a) *
                (dimPoint / (float) flashes) *
                ((timePassed - dimPoints.get(dimPoint - 1)) /
                    (float) (dimPoints.get(dimPoint) - dimPoints.get(dimPoint - 1))));
    }

    private void findDimmingPoints() {
        dimPoints.clear();
        dimPoints.add(0);

        float onePart = 1 / (float) flashes * duration;
        float point = onePart / 2.0f;

        for (int i = 0; i < flashes; i++) {
            point = onePart + onePart * i;
            dimPoints.add((int) point);
        }
    }
}
